package ex2.employee;

public class CEquipment implements IEquipment {

	private String _name;
	private double _cost;
	
	
	public CEquipment(String record ) throws IllegalArgumentException{
		
		String tLeftRecord;
		
		try{
			
			//equipment name parsing
			this._name = _parser(record);	
			tLeftRecord = (record.substring(this._name.length()+1)).trim();
			
			//equipment cost parsing	
			double tParsedEquipmentCost = 0.0;
			String tStringtParsedEquipmentCost = _parser(tLeftRecord);
			try{
				tParsedEquipmentCost = Double.parseDouble(tStringtParsedEquipmentCost);
			}
			catch (NumberFormatException e){
				throw new IllegalArgumentException();
			}
			this._cost = tParsedEquipmentCost;
			
		}
		catch(StringIndexOutOfBoundsException e){
			throw new IllegalArgumentException();
		}
		
	}
	
	
	@Override
	public String getName() {
		return this._name;
	}

	@Override
	public double getCost() {
		return this._cost;
	}
	
	
	private String _parser(String record){
		
		String tTrimmedRecord = record.trim();
		int tIndexOfSeperator = tTrimmedRecord.indexOf(" ");
		String tAnswer;
		
		if (tIndexOfSeperator == -1)
			tAnswer = tTrimmedRecord.substring(0);
		else
			tAnswer = tTrimmedRecord.substring(0,tIndexOfSeperator);
		
		return tAnswer;
	}

}
